//计算年龄。
//码云的Syntax Highlight有点迷(?)，常量tzone里转义回车主要想读起来方便，不造这里为啥不能识别。Code::Blocks下用GCC(C99)编译运行正常。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define tzone "\
               Code/Time Zone/CUT Offset\n\
               CUT0GDT/Coordinated Universal Time(格林威治时间)/CUT\n\
               GMT0BST/United Kingdom(伦敦,都柏林,里斯本,爱丁堡)/CUT\n\
               AZOREST1AZOREDT/Azores,Cape Verde(亚速尔群岛,佛得角群岛)/CUT -1\n\
               FALKST2FALKDT/Falkland Islands(福克兰群岛(西大西洋))/CUT -2\n\
               RNLNDST3GRNLNDDT/Greenland,East Brazil(格陵兰,巴西利亚,纽芬兰)/CUT -3\n\
               AST4ADT/Central Brazil(大西洋时间(加拿大),南美洲西部,圣地亚哥)/CUT -4\n\
               EST5EDT/Eastern United States,Colombia(东部时间(美加),印第安纳(东部))/CUT -5\n\
               CST6CDT/Central United States,Honduras(中部时间(美加),墨西哥城)/CUT -6\n\
               MST7MDT/Mountain United States(山地时间(美加),亚利桑那)/CUT -7\n\
               PST8PDT/Pacific United States,Yukon(太平洋时间(美加),提华纳)/CUT -8\n\
               AST9ADT/Alaska(阿拉斯加)/CUT -9\n\
               HST10HDT/Hawaii,Aleutian Islands(夏威夷)/CUT -10\n\
               BST11BDT/Bering Strait(中途岛,萨摩亚群岛)/CUT -11\n\
               NZST-12NZDT/New Zealand(新西兰)/CUT +12\n\
               MET-11METDT/Solomon Islands(太平洋中部,所罗门群岛)/CUT +11\n\
               EET-10EETDT/Eastern Australia(堪培拉,墨尔本,悉尼,关岛)/CUT +10\n\
               JST-9JSTDT/Japan(东京)/CUT +9\n\
               KORST-9KORDT/Korea(首尔,大阪,札幌)/CUT +9\n\
               WAUST-8WAUDT/Western Australia(北京,上海,台湾,香港,新加坡)/CUT +8\n\
               TAIST-8TAIDT/Taiwan(台湾(中国))/CUT +8\n\
               THAIST-7THAIDT/Thailand(曼谷,河内,雅加达,胡志明市)/CUT +7\n\
               TASHST-6TASHDT/Central Asia(中亚,阿马提,达卡)/CUT +6\n\
               PAKST-5PAKDT/Pakistan(伊斯兰堡,卡拉奇)/CUT +5\n\
               WST-4WDT/Gorki,Central Asia,Oman(高尔基,中亚,阿曼)/CUT +4\n\
               MEST-3MEDT/Turkey(土耳其)/CUT +3\n\
               SAUST-3SAUDT/Saudi Arabia(沙特阿拉伯)/CUT +3\n\
               WET-2WET/Finland(芬兰)/CUT +2\n\
               USAST-2USADT/South Africa(南非,东欧)/CUT +2\n\
               NFT-1DFT/Norway(挪威,罗马)/CUT +1\n\
               "
_Bool LEGAL(struct tm*local,struct tm birth)
{
    if(birth.tm_year > local->tm_year)
        return 0;
    else if(birth.tm_year == local->tm_year){
        if(birth.tm_mon > local->tm_mon)
            return 0;
        else if(birth.tm_mon == local->tm_mon){
            if(birth.tm_mday > local->tm_mday)
                return 0;
            else if(birth.tm_mday == local->tm_mday){
                if(birth.tm_hour > local->tm_hour)
                    return 0;
                else if(birth.tm_hour == local->tm_hour){
                    if(birth.tm_min > local->tm_min)
                        return 0;
                    else if(birth.tm_min == local->tm_min){
                        if(birth.tm_sec > local->tm_sec)
                            return 0;
                    }
                }
            }
        }
    }
    return 1;
}
int main()
{
    char tzstr[20]="TZ=WAUST-8WAUDT";
    char timenow[100];
    time_t t;
    struct tm *local,birth;
    int f;
    double age;
    int deltayear;
    double deltasec;
    int i;
    while(1){
        printf("Change Time Zone? 1(YES)/0(NO): ");
        scanf("%d",&f);
        if(f){
            puts("FYI");
            printf(tzone);
            printf("Code: ");
            scanf("%s",tzstr+3);
        }
        putenv(tzstr);
        tzset();
        t=time(NULL);
        local=localtime(&t);
        printf("Now: %s",asctime(local));
        //printf("%d %d %d %d %d %d\n",local->tm_year,local->tm_mon,local->tm_mday,local->tm_hour,local->tm_min,local->tm_sec);
        printf("your birth\nyear month day\n");
        scanf("%d%d%d",&birth.tm_year,&birth.tm_mon,&birth.tm_mday);
        printf("hour minute second (if not known,input 0):\n");
        scanf("%d%d%d",&birth.tm_hour,&birth.tm_min,&birth.tm_sec);
        //printf("%d %d %d %d %d %d\n",birth.tm_year,birth.tm_mon,birth.tm_mday,birth.tm_hour,birth.tm_min,birth.tm_sec);
        birth.tm_year -= 1900;
        birth.tm_mon += 11;
        birth.tm_mon %= 12;
        birth.tm_hour %= 24;
        birth.tm_min %= 60;
        birth.tm_sec %= 60;
        if(LEGAL(local,birth)){
            deltayear=0;
            deltasec=0;
            if((birth.tm_year)<70){
                deltayear=70-(birth.tm_year);
                for(i=birth.tm_year+1900;i<1970;i++)
                    if(i%400==0||(i%4==0&&(i%100!=0)))
                        deltasec+=366*24*60*60;
                    else
                        deltasec+=365*24*60*60;
                birth.tm_year=70;
            }
            age=difftime(mktime(local),mktime(&birth));
            printf("You are %d years old now.\nAnd you have lived for %.0f seconds exactly.",deltayear+(int)(age/3600/24/365),age+deltasec);
            break;
        }
        else{
            puts("Not born yet!");
        }
    }
    free(local);
    return 0;
}
